package game;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * Used to load game settings.
 */
public class GameSettings {
	public static final String INIT_FNAME = "init.txt";
	public static final String ROOMS_DIRECTORY_NAME = "rooms";

	public GameSettings() {
		FileSystem fs = FileSystems.getDefault();
		String wd = System.getProperty("user.dir");
		workingDirectory = fs.getPath(wd);

		setToDefault();
	}

	public void load() {
		Path initPath = workingDirectory.resolve(INIT_FNAME);

		// try to load init.txt
		if (!Files.exists(initPath)) {
			// create default init
			setToDefault();
			writeInitFile(initPath);
		} else {
			BufferedReader reader = null;
			try {
				reader = Files.newBufferedReader(initPath, Charset.defaultCharset());
				JSONObject init = new JSONObject(new JSONTokener(reader));
				setDataDirectoryName(init.getString("dataDirectory"));
			} catch (IOException e) {
				System.out.println("Failed to read " + INIT_FNAME + ": " + e.getMessage());
				setToDefault();
			} catch (JSONException e) {
				System.out.println("Failed to read " + INIT_FNAME + ": " + e.getMessage());
				setToDefault();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {}
				}
			}
		}
	}

	private void setToDefault() {
		setDataDirectoryName("data");
	}

	private void writeInitFile(Path path) {
		JSONObject init = new JSONObject();
		init.put("dataDirectory", dataDirectoryName);

		BufferedWriter writer = null;
		try {
			writer = Files.newBufferedWriter(path, Charset.defaultCharset());
			init.write(writer);
		} catch (IOException e) {
			System.out.println("Failed to write " + INIT_FNAME + ": " + e.getMessage());
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {}
			}
		}

		// create file structure
		try {
			Files.createDirectory(dataDirectory);
			Files.createDirectory(roomsDirectory);
		} catch (IOException e) {
			System.out.println("Failed to create directory structure with data directory '" + dataDirectoryName + "': " + e.getMessage());
		}
	}

	public String getDataDirectoryName() {
		return dataDirectoryName;
	}

	public void setDataDirectoryName(String dataDirectoryName) {
		this.dataDirectoryName = dataDirectoryName;
		dataDirectory = workingDirectory.resolve(this.dataDirectoryName);
		roomsDirectory = dataDirectory.resolve(ROOMS_DIRECTORY_NAME);
	}

	public Path getWorkingDirectory() {
		return workingDirectory;
	}

	public Path getDataDirectory() {
		return dataDirectory;
	}

	public Path getRoomsDirectory() {
		return roomsDirectory;
	}

	private String dataDirectoryName;

	private Path workingDirectory;
	private Path dataDirectory;
	private Path roomsDirectory;
}
